/*
@Time : 2021/2/5 14:32
@Author : 吴周海
@Mail: wzh200x@126.com
@File : common.go
@Software: GoLand
*/
package utils

import (
	_interface "GameApiServer/interface"
	"crypto/md5"
	"encoding/hex"
	"github.com/beego/beego/v2/core/config"
	"sort"
	"strings"
	"time"
)

// MD5加密生成大写
func Md5Encode(info string) string {
	h := md5.New()
	h.Write([]byte(info))
	return strings.ToUpper(hex.EncodeToString(h.Sum(nil)))
}

// 生成效验字符串
func GenerateSignStr(params map[string]interface{}) string {
	var keys []string
	for k := range params {
		keys = append(keys, k)
	}
	// key排序
	sort.Strings(keys)
	generateStr := ""
	// 参数拼接
	for _, k := range keys {
		generateStr += k + "=" + params[k].(string) + "&"
	}
	return generateStr
}

// 校验请求参数
func VerifyRequestData(reInfo _interface.IRequestInfo) bool {
	// 生成拼接验证字符串
	generateStr := GenerateSignStr(reInfo.FormatSignParams())
	// 从配置中获取秘钥
	secretKey, _ := config.String("requestCheckKey")
	// 拼接秘钥
	generateStr += secretKey
	// md5加密
	secretData := Md5Encode(generateStr)
	// 比对信息
	return secretData == reInfo.GetSign()
}

// 获取格式化的时间
func GetFormatTime() (int64, string) {
	startTimestamp := time.Now().Unix() //获得时间戳
	startTimeStr := time.Unix(startTimestamp, 0).Format("2006-01-02 15:04:05")
	return startTimestamp, startTimeStr
}
